package com.qk.management.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.BizEnum;
import com.qk.common.exception.BizException;
import com.qk.dto.course.AddCourseDTO;
import com.qk.dto.course.QueryPageCourseDTO;
import com.qk.dto.course.UpdateCourseDTO;
import com.qk.entity.Business;
import com.qk.entity.Course;
import com.qk.entity.Customer;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:gzc
 * @Date: 2025/10/06 20:56
 * @Description:
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<Course> queryListByPage(QueryPageCourseDTO queryPageCourseDTO) {
//        PageInfo<Course> pageInfo = PageHelper.startPage(courseQueryListByPageDto.getPage(), courseQueryListByPageDto.getPageSize())
//                .doSelectPageInfo(() -> courseMapper.queryPage(courseQueryListByPageDto));
//        return PageResult.<Course>builder().total(pageInfo.getTotal()).rows(pageInfo.getList()).build();
//        Page<Course> page = new Page<>(queryPageCourseDTO.getPage(), queryPageCourseDTO.getPageSize());
//        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.apply(StringUtils.hasText(queryPageCourseDTO.getName()),
//                        "replace(course.name, ' ', '') like concat('%',replace({0}, ' ', ''), '%')", queryPageCourseDTO.getName())
//                .eq(queryPageCourseDTO.getSubject() != null, Course::getSubject, queryPageCourseDTO.getSubject())
//                .eq(queryPageCourseDTO.getTarget() != null, Course::getTarget, queryPageCourseDTO.getTarget())
//                .orderByDesc(Course::getUpdateTime);
//        IPage<Course> iPage = courseMapper.selectPage(page, queryWrapper);
        Page<Course> page = this.page(new Page<>(queryPageCourseDTO.getPage(), queryPageCourseDTO.getPageSize()), Wrappers.lambdaQuery(Course.class)
                .apply(StringUtils.hasText(queryPageCourseDTO.getName()),
                        "replace(course.name, ' ', '') like concat('%',replace({0}, ' ', ''), '%')", queryPageCourseDTO.getName())
                .eq(queryPageCourseDTO.getSubject() != null, Course::getSubject, queryPageCourseDTO.getSubject())
                .eq(queryPageCourseDTO.getTarget() != null, Course::getTarget, queryPageCourseDTO.getTarget())
                .orderByDesc(Course::getUpdateTime));
        return PageResult.<Course>builder().total(page.getTotal()).rows(page.getRecords()).build();
    }

    @Override
    public void deleteById(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        List<Customer> customers = customerMapper.selectList(Wrappers.lambdaQuery(Customer.class).eq(Customer::getCourseId, id));
        if (!ObjectUtil.isEmpty(customers)) {
            BizException.throwBizException(BizEnum.CUSTOMER_EXIST);
        }
        List<Business> businesses = businessMapper.selectList(Wrappers.lambdaQuery(Business.class).eq(Business::getCourseId, id));
        if (!ObjectUtil.isEmpty(businesses)) {
            BizException.throwBizException(BizEnum.BUSINESS_EXIST);
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void addCourse(AddCourseDTO addCourseDTO) {
        boolean hasNull = BeanUtil.hasNullField(addCourseDTO, "description");
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Course course = BeanUtil.copyProperties(addCourseDTO, Course.class);
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        this.save(course);
    }

    @Override
    public Course queryById(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        return this.getById(id);
    }

    @Override
    public void updateCourse(UpdateCourseDTO updateCourseDTO) {
        boolean hasNull = BeanUtil.hasNullField(updateCourseDTO, "description", "createTime", "updateTime");
        if (hasNull) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        Course course = BeanUtil.copyProperties(updateCourseDTO, Course.class);
        course.setUpdateTime(LocalDateTime.now());
        this.updateById(course);
    }

    @Override
    public List<Course> queryAll() {
        return this.baseMapper.selectList(null);
    }

    @Override
    public List<Course> queryBySubject(Integer subject) {
        if (ObjectUtil.isEmpty(subject)) {
            BizException.throwBizException(BizEnum.PARAM_ERROR);
        }
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getSubject, subject);
        return this.baseMapper.selectList(queryWrapper);
    }
}
